package org.noote.libs.sogle.helper;

import org.noote.libs.sogle.Mesh;

import android.util.FloatMath;

public class Mesh_Cylinder extends Mesh {
	
	public Mesh_Cylinder(float fHeight, float fDiameter, int iSlice) {
		// TODO
		fHeight /=2;
		fDiameter /=2;
		iSlice = iSlice<2?2:iSlice;
		
		float[] vertices = new float[(iSlice+1)*2*3];
		short[] indices = new short[(iSlice)*3*2];
		
		float fSliceSize = (float) ((Math.PI*2)/iSlice);
		for(int s=0; s<iSlice+1; s++)
		{
			int idx = s*3;
			vertices[idx + 0] = FloatMath.cos(fSliceSize*s)*fDiameter;
			vertices[idx + 1] = fHeight;
			vertices[idx + 2] = FloatMath.sin(fSliceSize*s)*fDiameter;
			
			int idx2 = (s+iSlice+1)*3;
			vertices[idx2 + 0] = vertices[idx + 0];
			vertices[idx2 + 1] = -fHeight;
			vertices[idx2 + 2] = vertices[idx + 2];
		}
		
		for(int s=0; s<iSlice; s++)
		{
			int idx = s*6;
			
			// Triangle 1
			indices[idx + 0] = (short) (s);
			indices[idx + 1] = (short) (s + 1);
			indices[idx + 2] = (short) (s+iSlice+1+1);
			
			// Triangle 2
			indices[idx + 3] = indices[idx + 2];
			indices[idx + 4] = (short) (s+iSlice+1);
			indices[idx + 5] = indices[idx + 0];
		}
		
		setIndices(indices);
		setVertices(vertices);
		generateNormales();
	}
}
